package com.wjb.xietong.server.crm;

import java.util.Iterator;
import java.util.LinkedList;

import com.wjb.xietong.server.MsgNotify;
import com.wjb.xietong.server.Msg;
import com.wjb.xietong.server.crm.SalesInfo.SalesData;
import com.wjb.xietong.server.crm.SellerInfo.Seller;

public class CrmService{

	private LinkedList<Seller> sellerList = null;
	
	
	private static CrmService instance = null;
	
	private SellerInfo sellerInfo = null;
	private SellerStatis sellerStatis = null;
	private SalesInfo salesInfo = null;
	
	private MsgNotify fNotify = null;
	
	private CrmService(){
		sellerInfo = new SellerInfo();
		sellerList = new LinkedList<Seller>();
		
		sellerStatis = new SellerStatis();
		salesInfo = new SalesInfo();
	}
	
	public int setNotify(MsgNotify msgNotify){
		fNotify = msgNotify;
		return 0;
	}
	public static CrmService getInstance() {  
		if (instance == null) {  
			instance = new CrmService();  
		}  
		return instance;  
	}
	
	private class HttpRqstSeller implements Runnable{

		@Override
		public void run() {
			LinkedList<Seller> sellerListGet = new LinkedList<Seller>();
			Msg msg = sellerInfo.requestSellerInfo(sellerListGet);
			if (msg.msg == Msg.MSG_SUCCESS){
				sellerList = sellerListGet;
				
			}
			
			if (fNotify != null){
				fNotify.message(msg);
			}
		}
		
	}
	
	private class HttpRqstSales implements Runnable{

		private Seller seller = null;
		private String date = null;
		@Override
		public void run() {
			LinkedList<SalesData> listSales = new LinkedList<SalesData>();
			Msg msg = salesInfo.requestInfo(listSales, seller.id, date);
			if (msg.msg == Msg.MSG_SUCCESS){
				
				seller.listSales = listSales;

			}
			else {
				
			}
			
			if (fNotify != null){
				fNotify.message(msg);
			}
				
		}
		public int setParam(Seller sl, String dt){
			seller = sl;
			date = dt;
			return 0;
		}
	}
	
	private class HttpRqstStatis implements Runnable{

		private Seller seller = null;
		private String date = null;
		@Override
		public void run() {
			Msg msg = sellerStatis.requestSellerInfo(seller.sellerStatis, seller.id, date);
			if (msg.msg == Msg.MSG_SUCCESS){

			}
			else {
				
			}
			
			if (fNotify != null){
				fNotify.message(msg);
			}
				
		}
		public int setParam(Seller sl, String dt){
			seller = sl;
			date = dt;
			return 0;
		}
	}
	public int requestSeller(){
		
		HttpRqstSeller rqst = new HttpRqstSeller();
		Thread t = new Thread(rqst);
		t.start();
		
		return 0;
	}
	public Seller getSeller(int index){
		Seller slRet = null;
		if (sellerList == null){
			return null;
		}
		Iterator<Seller> it = sellerList.iterator();
		int count = 0;
		while (it.hasNext()){
			Seller sl = it.next();
			if (count == index){
				slRet = sl;
			}
			count++;
		}
		return slRet;
	}
	
	public Seller getSeller(String name){
		Seller slRet = null;
		if (sellerList == null){
			return null;
		}
		Iterator<Seller> it = sellerList.iterator();
		while (it.hasNext()){
			Seller sl = it.next();
			if (sl.equals(name)){
				slRet = sl;
				break;
			}
		}
		return slRet;
	}
	public int requestSellerCrm(Seller seller, String date){
		
		HttpRqstStatis statis = new HttpRqstStatis();
		statis.setParam(seller, date);
		Thread tStatis = new Thread(statis);
		tStatis.start();
		
//		HttpRqstSales sales = new HttpRqstSales();
//		sales.setParam(seller, date);
//		Thread tSales = new Thread(sales);
//		tSales.start();
		
		return 0;
	}
	
	public int requestTopSales(Seller seller, String date){
		
		HttpRqstSales sales = new HttpRqstSales();
		sales.setParam(seller, date);
		Thread tSales = new Thread(sales);
		tSales.start();
		
		return 0;
	}
	
	public LinkedList<Seller> getSeller(){
		return sellerList;
	}
}
